﻿--[[
   @brief    contact list view
   @author   perpu
   @version  1.0
   @date     2016-06-15
--]]

contact_list_view = 
{
    max_mode_group_list_ctrl_id = 0;
    mini_mode_group_list_ctrl_id = 0;

    max_mode_friend_list_ctrl_id = 0;
    mini_mode_friend_list_ctrl_id = 0;

    social_tips_param = nil;
    social_main_dialog_id = 0;
    contract_list_parent_id = 0;

    first_click_contact_tab = true;
}

function contact_list_view.init()
    base_utility.log("[contact_list_view] init ");
end

function contact_list_view.is_wegamehk()
    return base_utility.is_type_of_oversea_ver();
end

--内部调用函数！！
function contact_list_view.show_friend_panel(is_show)
    local list_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_FriendList");
    local btn_icon_right_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_F\\Btn_FriendListTitle\\Btn_Icon_Right");
    local btn_icon_down_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_F\\Btn_FriendListTitle\\Btn_Icon_Down");
    if is_show then
        ui_utility.setproperty_by_id(list_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "true");
        contact_list_view.update_f_list_size();
    else
        ui_utility.setproperty_by_id(list_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "false");
    end
end

function contact_list_view.show_disc_panel(is_show)
    local list_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_RoomList");
    local btn_icon_right_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_Room\\Btn_RoomListTitle\\Btn_Icon_Right");
    local btn_icon_down_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_Room\\Btn_RoomListTitle\\Btn_Icon_Down");
    if is_show then
        ui_utility.setproperty_by_id(list_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "true");
        contact_list_view.update_d_list_size();
    else
        ui_utility.setproperty_by_id(list_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "false");
    end
end

function contact_list_view.show_group_panel(is_show)
    local list_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Ctn_GroupList");
    local btn_icon_right_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_Group\\Btn_GroupListTitle\\Btn_Icon_Right");
    local btn_icon_down_id = ui_utility.get_ctrl_id_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Container_Group\\Btn_GroupListTitle\\Btn_Icon_Down");
    if is_show then
        ui_utility.setproperty_by_id(list_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "true");
        contact_list_view.update_g_list_size();
    else
        ui_utility.setproperty_by_id(list_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "false");
    end
end

function contact_list_view.click_group_title(uilib, ctrl_id)
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Ctn_GroupList");
    if nil == list_ptr then
        return;
    end

    if TRUE == list_ptr:IsVisible() then
        contact_list_view.show_group_panel(false);
    else
        contact_list_view.show_group_panel(true);
        contact_list_view.show_friend_panel(false);
        contact_list_view.show_disc_panel(false);

        comm_center.call_service('i_service_tqos_socail_group_title_click', {});
    end
end

function contact_list_view.click_disc_title(uilib, ctrl_id)
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_RoomList");
    if nil == list_ptr then
        return;
    end

    if TRUE == list_ptr:IsVisible() then
        contact_list_view.show_disc_panel(false);
    else
        contact_list_view.show_group_panel(false);
        contact_list_view.show_friend_panel(false);
        contact_list_view.show_disc_panel(true);

        comm_center.call_service('i_service_tqos_socail_disc_title_click', {});
    end
end

function contact_list_view.click_friend_title(uilib, ctrl_id)
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_FriendList");
    if nil == list_ptr then
        return;
    end

    if TRUE == list_ptr:IsVisible() then
        contact_list_view.show_friend_panel(false);
    else
        contact_list_view.show_group_panel(false);
        contact_list_view.show_friend_panel(true);
        contact_list_view.show_disc_panel(false);

        comm_center.call_service('i_service_tqos_socail_friend_title_click', {});
    end
end

function contact_list_view.open_friend_list(data)
    if contact_list_view.max_mode_friend_list_ctrl_id > 0 then
        local ptr = ui_utility.get_ctrl_ptr_by_id(contact_list_view.max_mode_friend_list_ctrl_id);
        local p_id = ui_utility.get_ctrl_parent_id(contact_list_view.max_mode_friend_list_ctrl_id);

        contact_list_view.update_title_ui();

        if ptr:IsVisible() == FALSE then
            local title_id = 0;
            title_id = ui_utility.get_ctrl_id_by_path(p_id, "Container_F\\Btn_FriendListTitle");
            if title_id > 0 then
                contact_list_view.click_friend_title(ui_utility.uilib(), title_id);
            end
        end
    end
    return {};
end

function contact_list_view.get_current_show_list(data)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    local g_list_id = 0;
    local f_list_id = 1
    if data.is_show == 1 then --max mode
        g_list_id = contact_list_view.max_mode_group_list_ctrl_id;
        f_list_id = contact_list_view.max_mode_friend_list_ctrl_id;
    else
        g_list_id = contact_list_view.mini_mode_group_list_ctrl_id;
        f_list_id = contact_list_view.mini_mode_friend_list_ctrl_id;
    end

    if g_list_id > 0 then
        local ptr = ui_utility.get_ctrl_ptr_by_id(g_list_id);
        if ptr:IsVisible() == TRUE then
            return {show_list = 1};
        end
    end
    if f_list_id > 0 then
        local ptr = ui_utility.get_ctrl_ptr_by_id(f_list_id);
        if ptr:IsVisible() == TRUE then
            return {show_list = 2};
        end
    end
    return {show_list = 0};
end

function contact_list_view.show_btn_icon(btn_icon_right_id, btn_icon_down_id, list_show)
    if list_show then
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "false");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "true");
    else
        ui_utility.setproperty_by_id(btn_icon_right_id, "Visible", "true");
        ui_utility.setproperty_by_id(btn_icon_down_id, "Visible", "false");
    end
end

function contact_list_view.on_group_list_create(uilib, ctrl_id)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 0 then --mini mode
        contact_list_view.mini_mode_group_list_ctrl_id = ctrl_id;   
    end
end

function contact_list_view.group_list_show(uilib, ctrl_id)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 1 then --max mode
        contact_list_view.max_mode_group_list_ctrl_id = ctrl_id;
        contact_list_view.update_g_list_size();      
    else -- mini mode
        if contact_list_view.is_adapt_group_list == nil then
            contact_list_view.is_adapt_group_list = true;
            contact_list_view.mini_mode_group_list_ctrl_id = ctrl_id;      
            --调整mini模式下的列表高度  h = 318
            local ptr = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
            local rc = ptr:GetRectEx(FALSE,TRUE);
            rc.bottom = rc.top + 318;
            ptr:MoveEx(rc,TRUE);

            if contact_list_view.social_tips_param ~= nil then
                contact_list_view.on_social_tips_show(social_event.SOCIAL_TIPS_SHOW,
                                                  base_utility.table_to_json(contact_list_view.social_tips_param));
            end
        end
    end 
end

--mini模式下需要监听社交推广tips的显示、隐藏
function contact_list_view.on_social_tips_show(event_id, json_str)
    local param = base_utility.json_to_table(json_str);

    if contact_list_view.mini_mode_group_list_ctrl_id > 0 then
        contact_list_view.social_tips_param = nil;
        local ptr = ui_utility.get_ctrl_ptr_by_id(contact_list_view.mini_mode_group_list_ctrl_id);
        local rc = ptr:GetRectEx(FALSE,TRUE);
        if param.is_show then
            if contact_list_view.is_adjust_high_for_show_tips == nil then
                rc.bottom = rc.bottom - param.height;
                contact_list_view.is_adjust_high_for_show_tips = true;
                ptr:MoveEx(rc,TRUE);
            end
        else
            if contact_list_view.is_adjust_high_for_show_tips ~= nil then
                rc.bottom = rc.bottom + param.height;
                ptr:MoveEx(rc,TRUE);
                contact_list_view.is_adjust_for_show_tips = nil;
            end
        end
    else
        if contact_list_view.social_tips_param == nil or param.is_show == true then
            contact_list_view.social_tips_param = param;
        elseif contact_list_view.social_tips_param ~= nil and contact_list_view.social_tips_param.is_show ~= param.is_show then
            contact_list_view.social_tips_param = nil;
        end
    end
end

function contact_list_view.friend_list_create(uilib, ctrl_id)
    contact_list_view.max_mode_friend_list_ctrl_id = ctrl_id;   
    contact_list_view.update_f_list_size();  
end

function contact_list_view.on_social_mainwnd_show(brd_data)
    if nil ~= brd_data and 0 == contact_list_view.social_main_dialog_id then
        contact_list_view.social_main_dialog_id = brd_data.main_dialog_id;
        contact_list_view.contract_list_parent_id = ui_utility.get_ctrl_id_by_path(
            contact_list_view.social_main_dialog_id,
            "Cnt_Tabs\\Ctn_MainFrameTab\\OverlapContainer_Tab\\UserObj_SocialContactList\\UserObj_SocialContactList_Imp\\Ctn_SocailListParent");
    end

    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 1 then
        contact_list_view.on_social_panel_pos_changed();
    end
end

function contact_list_view.on_social_panel_pos_changed(event_id, json_str)
    contact_list_view.update_f_list_size();
    contact_list_view.update_g_list_size();
    contact_list_view.update_d_list_size();
    if session_vlist_control ~= nil then
        session_vlist_control.fix_scrollbar_on_size_change();
    end;
    if friend_vlist_control ~= nil then
        friend_vlist_control.fix_scrollbar_on_size_change();
    end;
    if nil ~= contact_group_list_control then
        contact_group_list_control.fix_scrollbar_on_size_change();
    end;

    if nil ~= contact_discuss_list_control then
        contact_discuss_list_control.fix_scrollbar_on_size_change();
    end
end

function contact_list_view.update_g_list_size()
    local parent_ptr = ui_utility.get_ctrl_ptr_by_id(contact_list_view.contract_list_parent_id);
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Ctn_GroupList");
    if nil == parent_ptr or nil == list_ptr then
        return;
    end

    if FALSE == list_ptr:IsVisible() then
        return;
    end

    local parent_rc = parent_ptr:GetRectEx(FALSE,TRUE);
    local parent_h = parent_rc.bottom - parent_rc.top;

    local list_rc = list_ptr:GetRectEx(FALSE,TRUE);
    list_rc.top = 32 * 3;
    list_rc.bottom = list_rc.top + parent_h - 32*3;
    list_ptr:MoveEx(list_rc,TRUE);
end

function contact_list_view.update_d_list_size()
    local parent_ptr = ui_utility.get_ctrl_ptr_by_id(contact_list_view.contract_list_parent_id);
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_RoomList");
    if nil == parent_ptr or nil == list_ptr then
        return;
    end

    if FALSE == list_ptr:IsVisible() then
        return;
    end

    local parent_rc = parent_ptr:GetRectEx(FALSE,TRUE);
    local parent_h = parent_rc.bottom - parent_rc.top;

    local list_rc = list_ptr:GetRectEx(FALSE,TRUE);
    list_rc.top = 32 * 2;
    list_rc.bottom = list_rc.top + parent_h - 32*3;
    list_ptr:MoveEx(list_rc,TRUE);
end

function contact_list_view.update_f_list_size()
    local parent_ptr = ui_utility.get_ctrl_ptr_by_id(contact_list_view.contract_list_parent_id);
    local list_ptr = ui_utility.get_ctrl_ptr_by_path(
        contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_FriendList");
    if nil == parent_ptr or nil == list_ptr then
        return;
    end

    if FALSE == list_ptr:IsVisible() then
        return;
    end

    local parent_rc = parent_ptr:GetRectEx(FALSE,TRUE);
    local parent_h = parent_rc.bottom - parent_rc.top;

    local list_rc = list_ptr:GetRectEx(FALSE,TRUE);
    list_rc.top = 32;
    list_rc.bottom = list_rc.top + parent_h - 32*3;
    list_ptr:MoveEx(list_rc,TRUE);
end

function contact_list_view.on_friend_or_group_count_change(event_id, json_str)
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    local id = 0;
    if data.is_show == 1 then
        if contact_list_view.max_mode_contact_tab_id ~= nil then
            id = contact_list_view.max_mode_contact_tab_id;
        end
    else
        if contact_list_view.mini_mode_contact_tab_id ~= nil then
            id = contact_list_view.mini_mode_contact_tab_id;
        end
    end

    if id > 0 then
        contact_list_view.update_title_ui(id);
    end
end

function contact_list_view.click_on_contact_tab(event_id, json_str)
    local param = base_utility.json_to_table(json_str);
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    local id = param.contact_tab_id;
    if data.is_show == 1 then
        contact_list_view.max_mode_contact_tab_id = param.contact_tab_id;
        base_utility.log("[contact_list_view] max_mode_contact_tab_id set:"..tostring(contact_list_view.max_mode_contact_tab_id));
    else
        contact_list_view.mini_mode_contact_tab_id = param.contact_tab_id;
        base_utility.log("[contact_list_view] mini_mode_contact_tab_id set");
    end

    contact_list_view.update_title_ui(id);
    contact_list_view.update_group_list_ui(id);
end

function contact_list_view.update_title_ui(contact_tab_id)
    contact_list_view.get_contact_list_parent_id();
    local friend_title_id = ui_utility.get_ctrl_id_by_path(contact_list_view.contract_list_parent_id,
        "Container_list_panel\\Container_F\\Btn_FriendListTitle");
    local f_total_cnt = friend_list_model.get_friend_count();
    local s_my_friend = LoadStr("SOCIAL_CONTACT_VIEW_FRIEND");
    local s_my_room = LoadStr("SOCIAL_CONTACT_VIEW_ROOM");
    local s_my_group = LoadStr("SOCIAL_CONTACT_VIEW_GROUP");
    local str_f_cnt = string.format("%s(%d)", s_my_friend, f_total_cnt);
    ui_utility.setproperty_by_id(friend_title_id, "Caption", str_f_cnt);

    local disc_group_cnt, public_group_cnt = group_list_model.get_valid_group_count();

    local disc_title_id = ui_utility.get_ctrl_id_by_path(contact_list_view.contract_list_parent_id,
        "Container_list_panel\\Container_Room\\Btn_RoomListTitle");
    local str_disc_cnt = string.format("%s(%d)", s_my_room, disc_group_cnt);
    ui_utility.setproperty_by_id(disc_title_id, "Caption", str_disc_cnt);

    local group_title_id = ui_utility.get_ctrl_id_by_path(contact_list_view.contract_list_parent_id,
        "Container_list_panel\\Container_Group\\Btn_GroupListTitle");
    local str_group_cnt = string.format("%s(%d)", s_my_group, public_group_cnt);
    ui_utility.setproperty_by_id(group_title_id, "Caption", str_group_cnt);
end

function contact_list_view.get_friend_and_group_title_id(contact_tab_id)
    local ptr = ui_utility.get_ctrl_ptr_by_id(contact_tab_id);
    local pp_id = ptr:GetParent():GetParent();

    local str = "OverlapContainer_Tab\\UserObj_SocialContactList\\UserObj_SocialContactList_Imp\\Ctn_SocailListParent\\Container_1\\Container_F\\Btn_FriendListTitle";
    local ui_str, str_holder = U8toCUIStr(str);
    local f_btn_id = ui_utility.uilib():GetControlIDbyPath(ui_str, pp_id:GetControlID());

    local str = "OverlapContainer_Tab\\UserObj_SocialContactList\\UserObj_SocialContactList_Imp\\Ctn_SocailListParent\\Container_1\\Container_G\\Btn_GroupListTitle";
    local ui_str, str_holder = U8toCUIStr(str);
    local g_btn_id = ui_utility.uilib():GetControlIDbyPath(ui_str, pp_id:GetControlID());

    return f_btn_id, g_btn_id;
end


function contact_list_view.on_get_group_list_done(event_id, json_str)
    -- body
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddInterval('contact_list_view.on_process_list_done', 1);
end

function contact_list_view.on_process_list_done()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval('contact_list_view.on_process_list_done');

    contact_list_model.get_group_list_done = true;
    base_utility.log("[contact_list_view] on_process_list_done");

    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});
    if data.is_show == 1 then --max mode
        if contact_list_view.max_mode_contact_tab_id ~= nil then
            --max群列表已创建
            contact_list_view.update_group_list_ui(contact_list_view.max_mode_contact_tab_id);
        else
            base_utility.log("[contact_list_view] on_process_list_done max_mode_contact_tab_id is nil");
        end
    else
        if contact_list_view.mini_mode_contact_tab_id ~= nil then
            contact_list_view.update_group_list_ui(contact_list_view.mini_mode_contact_tab_id);
        end
    end
end

function contact_list_view.update_group_list_ui(contact_tab_id)
    if contact_list_model.get_group_list_done == true then
        local loading_id = ui_utility.get_ctrl_id_by_path(
            contact_list_view.contract_list_parent_id, "Container_list_panel\\Ctn_GroupList\\Ctn_Loading");
        ui_utility.setproperty_by_id(loading_id, "Visible", "false");
        local disc_loading_id = ui_utility.get_ctrl_id_by_path(
            contact_list_view.contract_list_parent_id, "Container_list_panel\\Obj_RoomList\\Ctn_Loading");
        ui_utility.setproperty_by_id(disc_loading_id, "Visible", "false");
        base_utility.log("[contact_list_view] update_group_list_ui");
    end
    
    -- contact_list_model.virtual_node_id_to_group_info = {}; bug http://tapd.oa.com/10041181/bugtrace/bugs/view?bug_id=1110041181056372746
    if false == contact_group_list_control.timer_started_ then
        contact_group_list_control.on_init(contact_list_view.contract_list_parent_id);
    end
    if contact_discuss_list_control.timer_started_ == false then
        contact_discuss_list_control.on_init(contact_list_view.contract_list_parent_id);
    end
end

function contact_list_view.on_loading_create(uilib, ctrl_id)
    if contact_list_model.get_group_list_done == true then
        ui_utility.setproperty_by_id(ctrl_id, "Visible", "false");
    end
end

function contact_list_view.on_click_change_recommend_groups(uilib, ctrl_id)
    comm_center.call_service("i_service_social_group_get_recommend_group", {});
end

function contact_list_view.on_click_recommend_groups_head(uilib, ctrl_id)
    local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local g_id = contact_list_model.join_group_btn_id_to_group_id[p_id];
    if g_id ~= nil then
        local param = {group_id = g_id}
        event_center.send_event(group_event.SHOW_GROUP_CENTER,base_utility.table_to_json(param));
    end
end

function contact_list_view.click_join_group_btn(uilib, ctrl_id)
    local p_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local ready_flag_id = ui_utility.get_ctrl_id_by_path(p_id, "TinyStatic_AlreadyFlag");

    ui_utility.setproperty_by_id(ctrl_id, "Visible", "false");
    ui_utility.setproperty_by_id(ready_flag_id, "Visible", "true");

    local g_id = contact_list_model.join_group_btn_id_to_group_id[p_id];
    if g_id ~= nil then
        contact_list_model.insert_apply_join_group_id(g_id);
    end
end

function contact_list_view.get_contact_list_parent_id()
    if 0 == contact_list_view.social_main_dialog_id then
        local data = comm_center.call_service("i_service_social_mainwnd_get_main_dialog_id", {});
        local list_parent_id = 0;
        if nil ~= data then
            contact_list_view.social_main_dialog_id = data.main_dialog_id;
            contact_list_view.contract_list_parent_id = ui_utility.get_ctrl_id_by_path(contact_list_view.social_main_dialog_id, 
                "Cnt_Tabs\\Ctn_MainFrameTab\\OverlapContainer_Tab\\UserObj_SocialContactList\\UserObj_SocialContactList_Imp\\Ctn_SocailListParent");
        end
    end
end

contact_list_view.init();

